package struts.action;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oracle.jrockit.jfr.RequestableEvent;
import com.xsoft.action.LoginAction;

import struts.base.BaseActionForm;
import struts.base.BaseAction;
import struts.utility.ViewFillFormData;
import struts.utility.AnnotationBean;

public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ActionServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		String url="";
		String servletPath=request.getServletPath(); // /register.do
		String path= this.getPath(servletPath); // /register
		Map<String, AnnotationBean> map = (Map<String, AnnotationBean>) this.getServletContext().getAttribute("strutsConfig");  
		AnnotationBean annotationBean = map.get(path); 
		String formBeanType = annotationBean.getFormBeanType();//实体类名称 formBean.RegisterForm
		BaseActionForm form = ViewFillFormData.fill(formBeanType, request);//使用表单数据填充实体类
		
		String actionType = annotationBean.getActionType();//业务处理类名称 action.RegisterAction
		BaseAction action = null;
		try {
			Class clazz = Class.forName(actionType);
			action=(BaseAction)clazz.newInstance();
			url=action.execute(request, form, annotationBean.getActionForwardMap());//执行业务逻辑 Java接口回调模式  模板模式
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		RequestDispatcher rd=request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	private String getPath(String servletPath){
		return servletPath.split("\\.")[0];
	}
}
